import Vue from 'vue'
import Router from 'vue-router'
import { fetchUserInfo } from '@/api/user'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    permission: 2,
    meta: { title: '用户登录' },
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    permission: 2,
    meta: { title: '404' },
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    permission: 2,
    component: Layout,
    redirect: '/home/index',
    hidden: true,
    meta: { title: '' }
  },
  {
    path: '/home',
    permission: 2,
    component: Layout,
    redirect: '/home/index',
    hidden: true,
    name: 'carousel',
    meta: { title: '容猫管理平台', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'index',
        name: 'index',
        component: () => import('@/views/home/welcome')
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/index',
    name: 'user',
    permission: 2,
    meta: { title: '用户管理', icon: 'el-icon-s-edit' },
    children: [
      {
        path: 'list',
        name: 'list',
        component: () => import('@/views/user/list'),
        meta: { title: '用户管理', icon: 'el-icon-user' }
      }
    ]
  },
  {
    path: '/newuser',
    component: Layout,
    permission: 2,
    redirect: '/newuser/index',
    name: 'newuser',
    meta: { title: '拉新管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'list',
        name: 'list',
        component: () => import('@/views/newuser/list'),
        meta: { title: '拉新管理', icon: 'el-icon-s-custom' }
      }
    ]
  },
  {
    path: '/product',
    component: Layout,
    permission: 2,
    redirect: '/product/index',
    name: 'product',
    meta: { title: '商品系统', icon: 'el-icon-s-goods' },
    children: [
      {
        path: 'list',
        name: 'list',
        component: () => import('@/views/product/list'),
        meta: { title: '商品列表', icon: 'el-icon-s-goods' }
      },
      {
        path: 'update',
        name: 'update',
        component: () => import('@/views/product/update')
        // meta: { title: '商品编辑', icon: 'el-icon-edit' }
      }
    ]
  },
  {
    path: '/credits',
    component: Layout,
    permission: 2,
    redirect: '/credits/index',
    name: 'credits',
    meta: { title: '积分系统', icon: 'el-icon-coin' },
    children: [
      {
        path: 'list',
        name: 'list',
        component: () => import('@/views/credits/list'),
        meta: { title: '积分流水表-C端', icon: 'el-icon-coin' }
      },
      {
        path: 'blist',
        name: 'blist',
        component: () => import('@/views/credits/blist'),
        meta: { title: '积分流水表-B端', icon: 'el-icon-coin' }
      },
      {
        path: 'withdraw',
        name: 'withdraw',
        component: () => import('@/views/credits/withdraw'),
        meta: { title: '提现申请处理', icon: 'el-icon-money' }
      }
    ]
  },
  {
    path: '/orders',
    component: Layout,
    permission: 2,
    redirect: '/orders/index',
    name: 'orders',
    meta: { title: '订单管理', icon: 'el-icon-s-order' },
    children: [
      {
        path: 'list',
        name: 'list',
        component: () => import('@/views/orders/list'),
        meta: { title: '订单系统', icon: 'el-icon-s-order' }
      },
      {
        path: 'expense',
        name: 'expense',
        hidden: true,
        component: () => import('@/views/orders/expense'),
        meta: { title: '查看消费记录', icon: 'el-icon-sold-out' }
      }
    ]
  },
  {
    path: '/verification',
    component: Layout,
    permission: 2,
    redirect: '/verification/index',
    name: 'verification',
    meta: { title: '核销系统', icon: 'el-icon-folder-checked' },
    children: [
      {
        path: 'list',
        name: 'list',
        component: () => import('@/views/verification/list'),
        meta: { title: '核销记录', icon: 'el-icon-folder-checked' }
      }
    ]
  },
  {
    path: '/groupbuy',
    component: Layout,
    permission: 2,
    redirect: '/groupbuy/index',
    name: 'groupbuy',
    meta: { title: '拼团管理', icon: 'el-icon-goods' },
    children: [
      {
        path: 'list',
        name: 'list',
        component: () => import('@/views/groupbuy/list'),
        meta: { title: '拼团一览表', icon: 'el-icon-goods' }
      },
      {
        path: 'edit',
        name: 'list',
        hidden: true,
        component: () => import('@/views/groupbuy/edit'),
        meta: { title: '新建/编辑拼团', icon: 'el-icon-edit' }
      },
      {
        path: 'orders',
        name: 'orders',
        component: () => import('@/views/groupbuy/orders'),
        meta: { title: '拼团订单', icon: 'el-icon-s-order' }
      },
      {
        path: 'records',
        hidden: true,
        name: 'records',
        component: () => import('@/views/groupbuy/records'),
        meta: { title: '消费记录', icon: 'el-icon-goods' }
      }
    ]
  },
  {
    path: '/plantproduct',
    component: Layout,
    permission: 2,
    redirect: '/plantproduct/index',
    name: 'plantproduct',
    meta: { title: '平台商品管理', icon: 'el-icon-s-goods' },
    children: [
      {
        path: 'list',
        name: 'list',
        component: () => import('@/views/plantproduct/list'),
        meta: { title: '商品列表', icon: 'el-icon-s-goods' }
      },
      {
        path: 'update',
        name: 'update',
        component: () => import('@/views/plantproduct/update')
        // meta: { title: '商品编辑', icon: 'el-icon-edit' }
      },
      {
        path: 'orders',
        name: 'orders',
        component: () => import('@/views/plantproduct/orders'),
        meta: { title: '平台商品订单', icon: 'el-icon-s-goods' }
      },
      {
        path: 'expense',
        name: 'expense',
        hidden: true,
        component: () => import('@/views/plantproduct/expense'),
        meta: { title: '查看核销记录', icon: 'el-icon-sold-out' }
      }
    ]
  },
  {
    path: '/authority',
    component: Layout,
    permission: 2,
    redirect: '/authority/index',
    name: 'authority',
    meta: { title: '权限管理', icon: 'el-icon-unlock' },
    children: [
      {
        path: 'accounts',
        name: 'accounts',
        component: () => import('@/views/authority/accounts'),
        meta: { title: '账号管理', icon: 'el-icon-unlock' }
      },
      {
        path: 'department',
        name: 'department',
        component: () => import('@/views/authority/department'),
        meta: { title: '业务部门管理', icon: 'el-icon-lock' }
      },
      {
        path: 'platformauth',
        name: 'platformauth',
        component: () => import('@/views/authority/platformauth'),
        meta: { title: '平台权限管理', icon: 'el-icon-lock' }
      }
    ]
  },
  {
    path: '/coupon',
    component: Layout,
    permission: 2,
    redirect: '/coupon/index',
    name: 'coupon',
    meta: { title: '优惠券管理', icon: 'el-icon-s-ticket' },
    children: [
      {
        path: 'coupon',
        name: 'coupon',
        component: () => import('@/views/coupon/list'),
        meta: { title: '优惠券管理', icon: 'el-icon-s-ticket' }
      },
      {
        path: 'platformauth',
        name: 'platformauth',
        component: () => import('@/views/coupon/records'),
        meta: { title: '使用记录', icon: 'el-icon-notebook-1' }
      }
    ]
  },
  {
    path: '/checking',
    component: Layout,
    permission: 2,
    redirect: '/checking/index',
    name: 'checking',
    meta: { title: '对账管理', icon: 'el-icon-s-cooperation' },
    children: [
      {
        path: 'normals',
        name: 'normals',
        component: () => import('@/views/checking/normals'),
        meta: { title: '普通订单', icon: 'el-icon-s-cooperation' }
      },
      {
        path: 'groups',
        name: 'groups',
        component: () => import('@/views/checking/groups'),
        meta: { title: '拼团订单', icon: 'el-icon-notebook-1' }
      },
      {
        path: 'plantform',
        name: 'plantform',
        component: () => import('@/views/checking/plantform'),
        meta: { title: '平台订单', icon: 'el-icon-s-cooperation' }
      }
    ]
  },
  { path: '*', redirect: '/404', hidden: true, meta: { title: '*' }, permission: 2 }
]

const userInfo = localStorage.getItem('userInfo')
const token = localStorage.getItem('token')

if (!userInfo || !token) {
  window.location.href = '/#/login'
}

// 路由权限过滤
const getBaseRoutes = (userPermission) => {
  constantRoutes.forEach((item) => {
    const perItem = userPermission.filter((per) => per.menu_name === item.meta.title)
    if (perItem.length > 0) {
      item.permission = perItem[0].permission_flag
    }
  })
  const baseRoutes = constantRoutes.filter((item) => item.permission > 0)
  return baseRoutes
}

const createRouter = () => {
  const userEntity = JSON.parse(localStorage.getItem('userInfo'))

  if (userEntity && userEntity.permission_list) {
    const userPermission = userEntity.permission_list
    return new Router({
      // mode: 'history',
      scrollBehavior: () => ({ y: 0 }),
      routes: getBaseRoutes(userPermission)
    })
  } else {
    const routers = constantRoutes.filter((item) => item.hidden)
    return new Router({
      // mode: 'history',
      scrollBehavior: () => ({ y: 0 }),
      routes: routers
    })
  }
}

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

fetchUserInfo({ token }).then((res) => {
  if (res.code === '0') {
    localStorage.setItem('userInfo', JSON.stringify(res.info.data))
    resetRouter()
  } else if (res.code === '9999') {
    this.$message.error(`${res.msg}`)
    window.location.href = '/#/login'
  }
})

export default router
